import React, { Component } from 'react'

export default class Demo extends Component {
  state = { count: 0 }
  increment = () => {
    /*     // 对象式的setState
        //1.获取原来的count值
        const { count } = this.state
        //2.更新状态
        this.setState({ count: count + 1 },()=>{
          console.log(this.state.count);
        })
        //console.log(this.state.count); // 输出为0 */

    //函数式的setState
    this.setState((state, props) => {
      console.log(state, props);
      return { count: state.count + 1 }
    })
  }
  render() {
    const { count } = this.state
    return (
      <div>
        <h1>求和为：{count}</h1>
        <button onClick={this.increment}>点我+1</button>
      </div>
    )
  }
}
